package com.byxy.ssm.demo.controller;

import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.byxy.ssm.demo.entity.User;
import com.byxy.ssm.demo.service.UserSerice;


import lombok.extern.slf4j.Slf4j;

@Controller
@RequestMapping("/")
@Slf4j//新的输出控制台方法
public class UserController {
	// 1. 显示页面
	// 2. 定义业务类与方法
	// 3. 定义Dao接口
	// 4. 单元测试业务接口
	// 5. 控制层与业务类整合
	// 6. UI层与控制整合
	@Autowired
	private UserSerice userSerice;
	@GetMapping("toLoginPage.do")//跳转到登录
	public String login(){
		return"login";
	}
	@PostMapping("userLogin.do")//登录
	public String doLogin(@RequestParam(required=false,defaultValue="0")int state, String  userName,String userPassword,String tipId,HttpSession session){
		User u = userSerice.getByNameAndPassword(userName, userPassword);
		log.info("userName="+userName);
		log.info("userPassword="+userPassword);
		log.info("user="+u);
		if (u==null) {
			session.setAttribute("loginUser", u);
			session.setAttribute("error", "用户名或者密码不正确");
			return"redirect:toLoginPage.do";
		}
		else{
			u.setUserPassword("");
			session.setAttribute("USER", u);
			session.removeAttribute("error");
			return"redirect:toMainPage.do";
		}
	}
	@GetMapping("toSignUpPage.do")//跳转到注册
	public String reg(){
		return"signUp";
	}
	
	@PostMapping("userSignUp.do")//注册
	public String doreg(String user_name,String user_nick,String user_password,Integer user_type){
		log.info("user_name = "+user_name);
		log.info("user_nick = "+user_nick);
		log.info("user_password = "+user_password);
		log.info("user_type = "+user_type);
		userSerice.save(user_name,user_nick,user_password,user_type);
		return"login";
	}
	
	@GetMapping("userSignOut.do")//退出登录
	public String doregtc(HttpSession session){
		session.invalidate();
		return"redirect:toLoginPage.do";
	}
	

}
